#!/bin/bash
[% c("var/set_default_env") -%]
[% pc(c('var/compiler'), 'var/setup', {
  compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')),
  gradle_tarfile => c("input_files_by_name/gradle"),
}) %]
distdir=/var/tmp/dist
builddir=/var/tmp/build
mkdir -p $builddir $distdir

tar -C $distdir -xf [% c('input_files_by_name/node') %]
export PATH="/var/tmp/dist/node/bin:$PATH"

tar -C $builddir -xf [% c('input_files_by_name/geckoview_armv7') %]
tar -C $builddir -xf [% c('input_files_by_name/geckoview_aarch64') %]
tar -C $builddir -xf [% c('input_files_by_name/geckoview_x86') %]
tar -C $builddir -xf [% c('input_files_by_name/geckoview_x86_64') %]
tar -C $builddir -xf [% project %]-[% c('version') %].tar.[% c('compress_tar') %]

# Specify the architectures we want to merge
export MOZ_ANDROID_FAT_AAR_ARCHITECTURES=armeabi-v7a,arm64-v8a,x86,x86_64
export MOZ_ANDROID_FAT_AAR_ARMEABI_V7A=$builddir/geckoview/*armeabi-v7a*.aar
export MOZ_ANDROID_FAT_AAR_ARM64_V8A=$builddir/geckoview/*arm64-v8a*.aar
# Specifying just "x86" is not differentiating enough
export MOZ_ANDROID_FAT_AAR_X86=$builddir/geckoview/*x86-*.aar
export MOZ_ANDROID_FAT_AAR_X86_64=$builddir/geckoview/*x86_64*.aar

cd $builddir/[% project %]-[% c("version") %]
ln -s mozconfig-android-all .mozconfig
cat >> mozconfig-android-all << 'MOZCONFIG_EOF'
mk_add_options MOZ_PARALLEL_BUILD=[% c("num_procs") %]
export MOZ_INCLUDE_SOURCE_INFO=1
export MOZ_SOURCE_REPO="[% c('var/gitlab_project') %]"
export MOZ_SOURCE_CHANGESET=[% c("var/git_commit") %]
MOZCONFIG_EOF

[% c("var/set_MOZ_BUILD_DATE") %]

export JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
gradle_repo=/var/tmp/dist/gradle-dependencies
export GRADLE_MAVEN_REPOSITORIES="file://$gradle_repo","file://$gradle_repo/maven2"
export GRADLE_FLAGS="--no-daemon --offline -Dmaven.repo.local=$distdir/[% project %]"
mv $rootdir/[% c('input_files_by_name/gradle-dependencies') %] $gradle_repo
cp -rl $gradle_repo/dl/android/maven2/* $gradle_repo || true
cp -rl $gradle_repo/m2/* $gradle_repo || true
cp -rl $gradle_repo/maven2/* $gradle_repo || true

# We unbreak mach, see: https://bugzilla.mozilla.org/show_bug.cgi?id=1656993.
export MACH_BUILD_PYTHON_NATIVE_PACKAGE_SOURCE=system
# Create .mozbuild to avoid interactive prompt in configure
mkdir "$HOME/.mozbuild"
# We still need to specify --tor-browser-version due to bug 34005.
./mach configure \
  --with-base-browser-version=[% c("var/torbrowser_version") %] \
  --enable-update-channel=[% c("var/variant") %] \
  [% IF !c("var/rlbox") -%]--without-wasm-sandboxed-libraries[% END %]

./mach build --verbose
# We prepare an archive to then extract it as we were using a local Maven
# repository.
# See also https://github.com/mozilla-mobile/firefox-android/blob/main/fenix/docs/substituting-local-gv.md
./mach gradle \
  geckoview:publishWithGeckoBinariesDebugPublicationToMavenLocal \
  exoplayer2:publishDebugPublicationToMavenLocal

cd $distdir/
[% c('tar', {
    tar_src => [ project ],
    tar_args => '-caf ' _ dest_dir _ '/' _ c('filename'),
  }) %]
